<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        *{padding:0;margin:0;box-sizing: border-box;}
        .box{
            width: 1000px;
            margin: 50px auto;
        }
        ul{
            list-style: none;
            display: flex;
        }
        li{
            flex: 1;
            background-color: bisque;
            padding: 10px;
        }
        li:hover{
            cursor: pointer;
        }
        /* .active{
            background-color: red;
        } */
    </style>
</head>

<body>
    <div id="root">
        <ul class="box" ref="box">
            <li :class="{active:item.id===currentId}" v-for="(item,index) in arr" :key="index" @click="liClick(index)" ref="index">
                {{item.title}}
            </li>
        </ul>
    </div>
    <script>
        const VM = new Vue({
            el: '#root',
            data() {
                return {
                    arr: [
                        { id: 1, title: '军事' },
                        { id: 2, title: '游戏' },
                        { id: 3, title: '直播' },
                        { id: 4, title: '财经' },
                        { id: 5, title: '汽车' },
                        { id: 6, title: '娱乐' },
                        { id: 7, title: '生活' },
                        { id: 8, title: '感情' },
                        { id: 9, title: '购物' },
                        { id: 10, title: '美食' },
                    ],
                    currentId:1
                };
            },
            methods: {
                ulClick(e){
                    console.log(e.target);
                    if(e.target.nodeName === 'LI')
                    e.target.style.backgroundColor = 'yellow'
                },
                liClick(index){
                    this.currentId=index;
                    // console.log(index);
                    console.log(this.$refs.index[index]);
                    // this.$refs[index][0].style.background="blue";
                    this.$refs.index[index].style.backgroundColor = 'blue';
                }
            },
        });
    </script>
</body>

</html>